/// <reference path="../../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../../Additions/Addition.js" />
/// <reference path="../../ExtenderDuplicator.debug.js" />
/// <reference path="../../Items/MItem.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

// TabbedDisplayAddition object

BM.TabbedDisplayAddition = function(item, additionNode) {
    BM.TabbedDisplayAddition.initializeBase(this, [item, additionNode]);    
    item.addFocusing(Function.createDelegate(this, this.raiseFocusing));
}

BM.TabbedDisplayAddition.prototype = {
    load: function() {
        BM.TabbedDisplayAddition.callBaseMethod(this, 'load');
        this.ensureTabExists();
        $(this.getTabNode()).click(Function.createDelegate(this, this.tabClicked));
        this.getItem().addDescriptionChanged(Function.createDelegate(this, this.descriptionChanged));        
    },

    descriptionChanged: function() {
        var tab = this.getTab();
        tab.setText(this.getItem().getDescription());
    },

    ensureTabExists: function() {
        this.createOrGetTabContainer();

        if (!this.getTabNode()) {
            var tab = this.addTab();
        }
    },

    tabClicked: function(event) {
        event.stopPropagation();
        this.getTab().choose();
        this.getItem().click();
    },

    raiseFocusing: function() {
        /// <summary>
        /// when 'focusing' event is raised on corresponding item
        /// the tab should be selected
        /// </summary>
        this.getTab().choose();
    },

    createOrGetTabContainer: function() {
        /// <summary>
        /// gets the existing tab container, or creates a new one if this one was not initiated 
        /// (e.g. when a MItem with tab container was just added)
        /// </summary>
        var tabContainer = $find(this.getTabContainerId());
        if (tabContainer == null) {
            var tabContainerNode = $get(this.getTabContainerId());
            if (tabContainerNode == null) {
                this.createTabContainerNode();
            }
            tabContainer = this.createTabContainer();
        }
        return tabContainer;
    },

    createTabContainerNode: function() {
        var div = document.createElement('div');
        div.setAttribute('id', this.getTabContainerId());
        div.setAttribute('class', BM.Tab.TabContainer);
        $(div).insertBefore($(this.getItem().getNode()).siblings()[0]);
        return div;
    },

    createTabContainer: function() {
        BM.ExtenderDuplicator.run(this.getTabContainerPrototypeId(), this.getTabContainerId());
        return this.getTabContainer();
    },

    getTabContainer: function() {
        return $find(this.getTabContainerId());
    },

    addTab: function() {
        var content = this.item.getDescription();
        content += BM.TabbedDisplayAddition.getImgText(this.getImagesPath() + "btn_close_1.gif", "active_btn", "BM.RemovalButton.switchImg(this, 2);", "BM.RemovalButton.switchImg(this, 1);", "BM.TabbedDisplayAddition.remove(this);");
        var tab = this.getTabContainer().add(this.item.getId(), null, content);
        return tab;
    },

    getImagesPath: function() {
        return this.getItem().getMForm().getImagesPath();
    },

    unload: function() {
        BM.TabbedDisplayAddition.callBaseMethod(this, 'unload');
        var tab = this.getTab();
        if (tab != null) {
            tab.dispose();
        }
    },

    getTabId: function() {
        return this.item.getId() + "__" + BM.Tab.Tab;
    },

    getTabNode: function() {
        var tc = this.getTabContainer();
        if (!tc) return null;
        return tc.getTab(this.getTabId());
    },

    getTab: function() {
        var tab = this.getTabNode();
        if (tab == null) return null;
        return BM.Tab.get(tab);
    },

    getTabContainerSuffix: function() {
        return this.additionNode.getAttribute('TCRef') || 'default';
    },

    getTabContainerId: function() {
        return this.item.getParentItem().getId() + '_' + this.getTabContainerSuffix();
    },

    getTabContainerPrototypeId: function() {
        return this.item.getParentItem().basis.node.id + '_' + this.getTabContainerSuffix();
    }

}

BM.TabbedDisplayAddition.registerClass("BM.TabbedDisplayAddition", BM.Addition);
BM.Addition['TabbedDisplayAddition'] = BM.TabbedDisplayAddition;

BM.TabbedDisplayAddition.remove = function(removalButton) {
    if (confirm(BM.Messages["ReallyDelete"])) {
        var tab = removalButton.parentNode.parentNode.parentNode;
        var tabItem = BM.Tab.get(tab);
        var item = MItem.getById(tabItem.getItemId());
        item.remove();
    }
}

BM.TabbedDisplayAddition.getImgText = function(src, className, onMouseOver, onMouseOut, onClick) {
    var builder = new Sys.StringBuilder("<img src=\"");
    builder.append(src);
    builder.append("\" class=\"");
    builder.append(className);
    builder.append("\"");
    if (onMouseOver) {
        builder.append(" onmouseover=\"");
        builder.append(onMouseOver);
        builder.append("\" ");
    }
    if (onMouseOut) {
        builder.append(" onmouseout=\"");
        builder.append(onMouseOut);
        builder.append("\" ");
    }
    if (onClick) {
        builder.append(" onclick=\"");
        builder.append(onClick);
        builder.append("\" ");
    }
    builder.append(">");
    return builder.toString();
}

